package CardDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    public static final String[] SUITS = {"♠", "♥", "♣", "♦"};
    //构造一副牌
    public List<Card> createCards() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(j, SUITS[i]);
                cardList.add(card);
            }
        }
        return cardList;
    }
    //洗牌
    public void shuffle(List<Card> cardList) {
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0; i--) {
            int indexRandom = random.nextInt(i);
            sway(cardList,i,indexRandom);
        }
    }
    private void sway(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }
    public List<List<Card>> play(List<Card> cardList) {
        List<List<Card>> hand = new ArrayList<>();

        List<Card> hand1= new ArrayList<>();
        List<Card> hand2= new ArrayList<>();
        List<Card> hand3= new ArrayList<>();
        
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        //3个人 每人发5张
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
